package com.whmskj.xjlhbc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whmskj.xjlhbc.domain.Bczl01Subczjlb;
import com.whmskj.xjlhbc.domain.EquipmentCounterStatements;
import com.whmskj.xjlhbc.domain.PersonnelManagementDataSheets;
import com.whmskj.xjlhbc.mapper.Bczl01SubczjlbMapper;
import com.whmskj.xjlhbc.mapper.EquipmentCounterStatementsMapper;
import com.whmskj.xjlhbc.mapper.PersonnelManagementDataSheetsMapper;
import com.whmskj.xjlhbc.service.IBczl01SubczjlbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 次轴记录表Service实现类
 */
@Service
public class Bczl01SubczjlbServiceImpl
        extends ServiceImpl<Bczl01SubczjlbMapper, Bczl01Subczjlb> implements IBczl01SubczjlbService {
    
    @Autowired
    private PersonnelManagementDataSheetsMapper personnelMapper;
    
    @Autowired
    private EquipmentCounterStatementsMapper equipmentMapper;
    
    @Override
    public List<String> getJiangShaWorkersByPosition(String position) {
        LambdaQueryWrapper<PersonnelManagementDataSheets> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(PersonnelManagementDataSheets::getPositions1, "浆纱")
               .eq(PersonnelManagementDataSheets::getActiveOrNot, "在职");
        
        List<PersonnelManagementDataSheets> personnelList = personnelMapper.selectList(wrapper);
        
        return personnelList.stream()
                .map(PersonnelManagementDataSheets::getFe1e)
                .filter(name -> name != null && !name.trim().isEmpty())
                .distinct()
                .collect(Collectors.toList());
    }
    
    @Override
    public List<String> getWeaverNumbers() {
        LambdaQueryWrapper<EquipmentCounterStatements> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(EquipmentCounterStatements::getStationNumber2)
               .eq(EquipmentCounterStatements::getDeviceName, "织布机")
               .isNotNull(EquipmentCounterStatements::getStationNumber2)
               .ne(EquipmentCounterStatements::getStationNumber2, "")
               .orderByAsc(EquipmentCounterStatements::getStationNumber2);
        
        List<EquipmentCounterStatements> equipmentList = equipmentMapper.selectList(wrapper);
        
        return equipmentList.stream()
                .map(EquipmentCounterStatements::getStationNumber2)
                .filter(number -> number != null && !number.trim().isEmpty())
                .distinct()
                .sorted((a, b) -> {
                    // 尝试按数字排序，如果无法转换为数字则按字符串排序
                    try {
                        Integer numA = Integer.parseInt(a);
                        Integer numB = Integer.parseInt(b);
                        return numA.compareTo(numB);
                    } catch (NumberFormatException e) {
                        return a.compareTo(b);
                    }
                })
                .collect(Collectors.toList());
    }
    
    @Override
    public Object getEquipmentByWeaverNumber(String weaverNumber) {
        LambdaQueryWrapper<EquipmentCounterStatements> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EquipmentCounterStatements::getStationNumber2, weaverNumber);
        
        return equipmentMapper.selectOne(wrapper);
    }
}
